<template>
  <view class="wrap">
    <uv-navbar :fixed="false" title="续费折扣" autoBack safeAreaInsetTop placeholder :border='false'>
      <template v-slot:left>
        <view class="navEditName nav-left">取消</view>
      </template>
      <template v-slot:right>
        <view class="navEditName nav-right" @click="tap">保存</view>
      </template>
    </uv-navbar>
    <view class="input">
      <uv-input placeholder="请输入" v-model="content" border="none" fontSize='32rpx' color='#000000' placeholderStyle='font-size: 32rpx; color: #D5D5D5'></uv-input>
    </view>
  </view>
</template>

<script setup>
import { ref, computed } from 'vue';
import { toast, editGroup, getGroup, division, mul } from '@/common';
import { useOrder } from '@/store';

let content = ref('');
const store = useOrder();
content.value = mul(store.group.discount_ratio, 10);

function tap(){
  if(!isValidNumber(content.value)) return toast('请输入正数且只能保留一位小数');
  if(Number(content.value) >= 10) return toast('请输入小于10的数字');
  const params = {
    id: store.group.id,
    discount_ratio: division(content.value, 10)
  }
  editGroup(params).then(res => {
    if(res.code == 1){
      toast('修改成功');
      getGroup(store.group.id);
      setTimeout(() => {
        uni.navigateBack({
          delta: 1
        });
      }, 1000)
    }
  })
}
function isValidNumber(input) {
  // 使用正则表达式判断是否为正数且最多保留一位小数
  const regex = /^\d+(\.\d{1})?$/;
  return regex.test(input);
}
</script>

<style lang="scss" scoped>
  .input{
    padding: 30rpx;
    background-color: #fff;
    margin-top: 20rpx;
  }
  .navEditName{
    font-weight: 400;
    font-size: 28rpx;
    &.nav-left{
      color: #737373;
    }
    &.nav-right{
      color: #E43222;
    }
  }
  .wrap{
    width: 100%;
    min-height: 100vh;
    background-color: #F5F5F5;
  }
</style>